package com.cos.hospital.model.parse;

import android.content.Context;

import com.cos.hospital.R;
import com.cos.hospital.dao.HelperAcuDb;
import com.cos.hospital.dao.beandao.DbAcupoint;
import com.cos.hospital.tool.ULog;
import com.cos.hospital.tool.UTxt;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年10月08日 16:40
 * @Email: KosmoSakura@foxmail.com
 */
public class URaw {
    private Context context;

    private URaw() {
    }

    public static URaw with(Context context) {
        URaw raw = new URaw();
        raw.context = context;
        return raw;
    }

    public void saveInDB() {
        String json = ioParseAcupoint(R.raw.acupoint);
        if (!UTxt.isEmpty(json)) {
            ArrayList<DbAcupoint> ls = UGson.toParses(json, DbAcupoint.class);
            ULog.commonD("Json解析结果：" + ls.size());
            HelperAcuDb.updateAll(context, ls);
        }
    }

    private String ioParseAcupoint(int res) {
        try {
            InputStreamReader isr = new InputStreamReader(context.getResources().openRawResource(res));
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
